//
//  BuyFlowWebViewController.m
//  HerconWaterFilter
//
//  Created by Kendama on 2017/5/12.
//  Copyright © 2017年 JianWei Chen. All rights reserved.
//

#import "WebViewController.h"
#import "IMYWebView.h"
#import "NavigatObject.h"

@interface WebViewController ()<UIWebViewDelegate,IMYWebViewDelegate>

@property (strong, nonatomic) IMYWebView *webview;
@property (weak, nonatomic) IBOutlet UIButton *backBtn;

@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavigationBarTitle:self.title];
    [self setLeftButtonImage:[UIImage imageNamed:@"nav_icon_back"]];
    self.navigationBar.backgroundColor = [UIColor colorWithHexString:@"3E76F6"];
    
    [self setWebData];
    


//    [self setNavigationTitle:@"意见反馈" titleColor:[UIColor whiteColor]];
//    [self setRightButtonTitle:@"提交"];
//    self.view.backgroundColor = [UIColor colorWithHexString:@"#F1F3F9"];
}

- (void)setWebData
{
    [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":@"Mozilla/5.0 AppleWebKit/537.36 Dtston/17.1.1"}];
    
    [_webview removeFromSuperview];
    _webview = [[IMYWebView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    [self.view addSubview:_webview];
    _webview.delegate=self;
    NSURLRequest *request = [NSURLRequest requestWithURL:self.url];
    [_webview loadRequest:request];
    
    if (self.type == 1) {
        NSString* path = [[NSBundle mainBundle] pathForResource:@"about" ofType:@"html"];
        NSURL* url = [NSURL fileURLWithPath:path];
        NSURLRequest* request = [NSURLRequest requestWithURL:url] ;
        [_webview loadRequest:request];
    } else if (self.type == 2) { // 用户协议
        [self setNavigationTitle:@"用户协议" titleColor:[UIColor colorWithHexString:@"#585D6D"]];
        [self setLeftButtonImage:[UIImage imageNamed:@"nav_icon_back_nor"]];
        self.navigationBar.backgroundColor = [UIColor whiteColor];
        NSString* path = [[NSBundle mainBundle] pathForResource:@"clause" ofType:@"html"];
        NSURL* url = [NSURL fileURLWithPath:path];
        NSURLRequest* request = [NSURLRequest requestWithURL:url] ;
        [_webview loadRequest:request];
    } else if (self.type == 3) {
        NSString* path = [[NSBundle mainBundle] pathForResource:@"help" ofType:@"html"];
        NSURL* url = [NSURL fileURLWithPath:path];
        NSURLRequest* request = [NSURLRequest requestWithURL:url] ;
        [_webview loadRequest:request];
    }
    
    //    [self.webview setHidden:YES];
    [self.view bringSubviewToFront:self.backBtn];
}

//- (void)onLeftButtonClick:(id)sender
//{
//    [[AppDelegate defaultService].menuViewController showLeftViewController];
//}

- (void)onRightButtonClick:(id)sender
{
    
}

- (IBAction)backAction:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}




-(void)webView:(IMYWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"didFailLoadWithError=====%@",error.description);
    
}
-(void)webViewDidFinishLoad:(IMYWebView *)webView
{
    
    NSLog(@"webViewDidFinishLoad=====%@",webView);
}
-(BOOL)webView:(IMYWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"===========shouldStartLoadWithRequest========%@====%ld",request.URL.absoluteString,(long)navigationType);
    
    NavigatObject *naviga=[[NavigatObject alloc]init];
    [naviga stringToHtmlString:request.URL.absoluteString];
    
    if ([naviga.Paramsdic[@"action"] isEqualToString:@"OPEN_WEBVIEW"]) {
        
        [self OPEN_WEBVIEW:naviga.Paramsdic];
        
        return NO;
    }else if ([request.URL.absoluteString rangeOfString:@"UPDATE_ADVICE"].location != NSNotFound){
        [self UPDATE_ADVICE:naviga.Paramsdic];
    }
    
    return YES;
}

- (void)OPEN_WEBVIEW:(NSDictionary *)paramsDic
{
    NSArray *nameArray = [paramsDic[@"url"] componentsSeparatedByString:@"."];
    WebViewController *vc = [[WebViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:nameArray[0] ofType:@".html"]];
    NSString *str = [url.absoluteString componentsSeparatedByString:[NSString stringWithFormat:@"%@%@",nameArray[0],@".html"]][0];
    url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",str,paramsDic[@"url"]]];
    vc.url = url;
    vc.title = paramsDic[@"title"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)UPDATE_ADVICE:(NSDictionary *)paramsDic
{
    [self getAdList:paramsDic[@"phone"] andConten:paramsDic[@"content"]];
}

- (void)getAdList:(NSString *)phone andConten:(NSString *)content
{
    //    UserFeedback * getApi = [[UserFeedback alloc] initWithPhone:phone andConten:content];
    //
    //    [getApi startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
    //        if ([getApi analyseResponseObject]) {
    //            [self.navigationController popViewControllerAnimated:YES];
    //        }
    //    } failure:^(YTKBaseRequest *request) {
    //        [SVProgressHUD showInfoWithStatus:@"网络异常"];
    //    }];
}


-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    
    NSLog(@"main dealloc  `");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
